﻿using System;
using System.Linq;
using Altairis.FluffyCloud.Data;

namespace Altairis.FluffyCloud.WebCore.Pages.My {

    public partial class Profile : System.Web.UI.Page {

        public User FormView_GetItem() {
            using (var dc = new FluffyDbContext()) {
                return dc.Users.Single(x => x.UserName.Equals(this.User.Identity.Name));
            }
        }

        public void FormView_UpdateItem() {
            using (var dc = new FluffyDbContext()) {
                var item = dc.Users.Single(x => x.UserName.Equals(this.User.Identity.Name));
                if (item == null) {
                    // The item wasn't found
                    ModelState.AddModelError("", "Uživatel neexistuje");
                    return;
                }
                TryUpdateModel(item);
                if (!ModelState.IsValid) return;
                try {
                    dc.SaveChanges();
                    this.PanelChangesSaved.Visible = true;
                }
                catch (Exception ex) {
                    this.ModelState.AddModelError("SaveError", ex);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
        }
    }
}